02dc51
@@ -157,7 +157,11 @@
public Object process(Node nd, Stack<Node> stack, NodeProcessorCtx procCtx,
       // the reduce sink key. Since both key columns are not prefix subset
       // ReduceSinkDeDuplication will not merge them together resulting in 2 MR jobs.
       // To avoid that we will remove the RS (and EX) inserted by enforce bucketing/sorting.
-      removeRSInsertedByEnforceBucketing(fsOp);
+      if (!removeRSInsertedByEnforceBucketing(fsOp)) {
+        LOG.debug("Bailing out of sort dynamic partition optimization as some partition columns " +
+            "got constant folded.");
+        return null;
+      }
 
       // unlink connection between FS and its parent
       Operator<? extends OperatorDesc> fsParent = fsOp.getParentOperators().get(0);
@@ -262,7 +266,7 @@
public Object process(Node nd, Stack<Node> stack, NodeProcessorCtx procCtx,
 
     // Remove RS and EX introduced by enforce bucketing/sorting config
     // Convert PARENT -> RS -> EX -> FS to PARENT -> FS
-    private void removeRSInsertedByEnforceBucketing(FileSinkOperator fsOp) {
+    private boolean removeRSInsertedByEnforceBucketing(FileSinkOperator fsOp) {
       HiveConf hconf = parseCtx.getConf();
       boolean enforceBucketing = HiveConf.getBoolVar(hconf, ConfVars.HIVEENFORCEBUCKETING);
       boolean enforceSorting = HiveConf.getBoolVar(hconf, ConfVars.HIVEENFORCESORTING);
@@ -297,17 +301,27 @@
private void removeRSInsertedByEnforceBucketing(FileSinkOperator fsOp) {
           Operator<? extends OperatorDesc> rsGrandChild = rsChild.getChildOperators().get(0);
 
           if (rsChild instanceof ExtractOperator) {
+            // if schema size cannot be matched, then it could be because of constant folding
+            // converting partition column expression to constant expression. The constant
+            // expression will then get pruned by column pruner since it will not reference to
+            // any columns.
+            if (rsParent.getSchema().getSignature().size() !=
+                rsChild.getSchema().getSignature().size()) {
+              return false;
+            }
             rsParent.getChildOperators().clear();
             rsParent.getChildOperators().add(rsGrandChild);
             rsGrandChild.getParentOperators().clear();
             rsGrandChild.getParentOperators().add(rsParent);
             parseCtx.removeOpParseCtx(rsToRemove);
             parseCtx.removeOpParseCtx(rsChild);
-            LOG.info("Removed " + rsParent.getOperatorId() + " and " + rsChild.getOperatorId()
+            LOG.info("Removed " + rsToRemove.getOperatorId() + " and " + rsChild.getOperatorId()
                 + " as it was introduced by enforce bucketing/sorting.");
           }
         }
       }
+
+      return true;
     }
 
     private List<Integer> getPartitionPositions(DynamicPartitionCtx dpCtx, RowSchema schema) {
